//
//  BQDetailViewController.m
//  BeeQuick
//
//  Created by AaronWei on 2017/7/11.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQDetailViewController.h"
#import "BQShopTableViewController.h"
#import "BQNaigationViewController.h"
#import "BQShopingCarView.h"
#import "BQProductModel.h"
#import "BQTopTableViewCell.h"
#import "BQMiddleTableViewCell.h"
#import "BQBottomTableViewCell.h"
#import "BQShopModelArrayManager.h"

@interface BQDetailViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) BQShopingCarView *shopingCarView;

@property (weak, nonatomic) UITableView *detailTableView;

@end

static NSString *topCell_id = @"topCell_id";

static NSString *middleHad_PmCell_id = @"middleHad_PmCell_id";

static NSString *middleHad_No_PmCell_id = @"middleHad_No_PmCell_id";

static NSString *bottomCell_id = @"bottomCell_id";

@implementation BQDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blueColor];
    
    self.navigationController.navigationBar.tintColor = [UIColor darkGrayColor];
    
    [self setupUI];
    
    [self registNotification];
    
}

- (void)registNotification {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didClickShopButton:) name:ToClickShopBtnNotificationName object:nil];
    
}

- (void)didClickShopButton:(NSNotification *)notification {
    
    BQShopTableViewController *shopVC = [[BQShopTableViewController alloc] init];
    
    BQNaigationViewController *nav = [[BQNaigationViewController alloc]initWithRootViewController:shopVC];
    
    [self presentViewController:nav animated:YES completion:nil];
    
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
- (void)setupUI {
    
    UITableView *detailTableView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    _detailTableView = detailTableView;
    
    detailTableView.dataSource = self;
    
    detailTableView.allowsSelection = NO;
    
    detailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    detailTableView.estimatedRowHeight = 100;
    
    detailTableView.rowHeight = UITableViewAutomaticDimension;
    
    [self.view addSubview:detailTableView];
    
    BQShopingCarView *shopingCarView = [BQShopingCarView shopingCarView];
    
    shopingCarView.redCount = [[BQShopModelArrayManager manager] shopManagerShowModelCount];
    
    _shopingCarView = shopingCarView;
    
    shopingCarView.model = self.model;
    
    [self.view addSubview:shopingCarView];
    
    [detailTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(shopingCarView.mas_top).offset(30);
    }];
    
    [shopingCarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.view);
        make.height.mas_equalTo(80);
    }];
    
    [detailTableView registerNib:[UINib nibWithNibName:@"BQTopTableViewCell" bundle:nil] forCellReuseIdentifier:topCell_id];
    
    [detailTableView registerNib:[UINib nibWithNibName:@"BQMiddleTableViewHadPmCell" bundle:nil] forCellReuseIdentifier:middleHad_PmCell_id];
    
    [detailTableView registerNib:[UINib nibWithNibName:@"BQMiddleTableViewHadNoPmCell" bundle:nil] forCellReuseIdentifier:middleHad_No_PmCell_id];
    
    [detailTableView registerNib:[UINib nibWithNibName:@"BQBottomTableViewCell" bundle:nil] forCellReuseIdentifier:bottomCell_id];;
    
    [detailTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        BQTopTableViewCell *topCell = [tableView dequeueReusableCellWithIdentifier:topCell_id forIndexPath:indexPath];
        
        topCell.model = self.model;
        
        return topCell;
        
    }
    
    if (indexPath.section == 1) {
        
        BQMiddleTableViewCell *middleCell;
        
        if (self.model.had_pm) {
            
            middleCell = [tableView dequeueReusableCellWithIdentifier:middleHad_PmCell_id forIndexPath:indexPath];
            
        } else {
            
            middleCell = [tableView dequeueReusableCellWithIdentifier:middleHad_No_PmCell_id forIndexPath:indexPath];
            
        }
        
        middleCell.model = self.model;
        
        return middleCell;
        
    }
        
    BQBottomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bottomCell_id forIndexPath:indexPath];
    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    return cell;
    
    
}

@end
